﻿using System;
using System.Data;
using System.Data.SqlClient;

using System.Web.UI.WebControls;
using NazSoft.Framework;

public partial class ApprovedUser : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //ddCity.Enabled = false;


        if (!Page.IsPostBack)
        {
            if (Request.QueryString["EmailId"] != null)
                txtEmail.Text = Request.QueryString["EmailId"];
            DataGridBind();
        }
    }

    private void DataGridBind()
    {
        object[] objName = { "UserType", "FirstName", "LastName", "Email", "isApproved", "isDenied" };

        Int32 UserTypeID = 0;
        if (ddUserType.SelectedValue != "")
            UserTypeID = Convert.ToInt32(ddUserType.SelectedValue);

        string strFirstName = txtFirstName.Text;
        string strLastName = txtLastName.Text;
        string strEmail = txtEmail.Text;

        Int16 IsApproved = 0;
        if (DropDownList1.SelectedValue != "")
            IsApproved = Convert.ToInt16(DropDownList1.SelectedValue);

        object[] objValue = { UserTypeID, strFirstName, strLastName, strEmail, IsApproved, chkDenied.Checked == true ? false : true };
        SqlHelper helpclass = new SqlHelper();
        DataTable dt = helpclass.GetDataTable("spUserFilter", objName, objValue);

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    protected void ddUserType_DataBound(object sender, EventArgs e)
    {
        ddUserType.Items.Insert(0, new ListItem("All UserType", "0"));
    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        DataGridBind();
    }

    protected void btnUpdateRecord_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            HiddenField ChangedOrNo = GridView1.Rows[i].FindControl("ChangedOrNo") as HiddenField;
            HiddenField UserId = GridView1.Rows[i].FindControl("UserId") as HiddenField;

            CheckBox ChkApproved = GridView1.Rows[i].FindControl("ChkApproved") as CheckBox;
            DropDownList dropdow = GridView1.Rows[i].FindControl("ddUserType") as DropDownList;

            if (ChangedOrNo.Value == "true")
            {
                object[] objName = { "UserId", "Approved", "UserTypeId" };
                object[] objValue = { UserId.Value, ChkApproved.Checked, dropdow.SelectedValue };
                SqlHelper helpclass = new SqlHelper();

                int result = helpclass.ExecuteNonQuery("SPUserApprovedOrNot", objName, objValue);

                if (result == 1)
                {
                    MessageLebal.Text = "User has been updated successfully!";
                    MessageLebal.Visible = true;
                }
                else
                {
                    MessageLebal.Text = "Unable to update due to some error!";
                    MessageLebal.Visible = true;
                }
            }
        }
        //GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView dataRowView = (DataRowView) e.Row.DataItem;
        //dataRowView.Row.ItemArray[]
        HiddenField ChangedOrNo = e.Row.FindControl("ChangedOrNo") as HiddenField;
        CheckBox ChkApproved = e.Row.FindControl("ChkApproved") as CheckBox;
        DropDownList dropdow = e.Row.FindControl("ddUserType") as DropDownList;
        if (ChkApproved != null)
            if (ChangedOrNo != null)
            {
                ChkApproved.Attributes.Add("onclick", "javascript:SetChangeValue(" + ChangedOrNo.ClientID.ToString() + ")");
            }

        if (dropdow != null)
        {
            if (ChangedOrNo != null)
            {
                dropdow.Attributes.Add("onchange", "javascript:SetChangeValue(" + ChangedOrNo.ClientID.ToString() + ")");
            }
            HiddenField UserTypeID = e.Row.FindControl("UserTypeId") as HiddenField;

            dropdow.SelectedValue = UserTypeID.Value;
        }

    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //pnlRights.Visible = true;
        //lblName.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text + " " +GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;

        //HiddenField UserId = GridView1.Rows[e.NewSelectedIndex].FindControl("UserId") as HiddenField;

        //hdUserId.Value = UserId.Value;

        //pnlRights.DataBind();
        //gvRights.DataBind();

    }

    protected void gvRights_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        HiddenField ChangedOrNo = e.Row.FindControl("ChangedOrNo") as HiddenField;
        CheckBox ChkApproved = e.Row.FindControl("ChkApproved") as CheckBox;

        if (ChkApproved != null)
            if (ChangedOrNo != null)
            {
                ChkApproved.Attributes.Add("onclick", "javascript:SetChangeValue(" + ChangedOrNo.ClientID.ToString() + ")");
            }
    }

   
    protected void lnkDyned_Command(object sender, CommandEventArgs e)
    {

        int UserID = Convert.ToInt32(e.CommandArgument);
        bool IsDenied = e.CommandName == "Accepted" ? true : false;

        SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["EPTSCConnectionString"].ToString());

        SqlCommand command = new SqlCommand();
        command.CommandText = "spUserDenied";
        command.CommandType = CommandType.StoredProcedure;
        command.Connection = connection;
        command.Parameters.AddWithValue("@UserId", Convert.ToInt32(UserID));
        command.Parameters.AddWithValue("@IsEnabled", IsDenied);

        connection.Open();
        int rowseffected = command.ExecuteNonQuery();
        connection.Close();

        DataGridBind();

    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable m_DataTable = GridView1.DataSource as DataTable;

        if (m_DataTable != null)
        {
            DataView m_DataView = new DataView(m_DataTable);
            m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridView1.DataSource = m_DataView;
            GridView1.DataBind();
        }

    }

    private string ConvertSortDirectionToSql(SortDirection sortDireciton)
    {
        string m_SortDirection = String.Empty;

        switch (sortDireciton)
        {
            case SortDirection.Ascending:
                m_SortDirection = "ASC";
                break;

            case SortDirection.Descending:
                m_SortDirection = "DESC";
                break;
        }
        return m_SortDirection;

    }


}
